﻿using JX.Enrollment.Entity;
using JX.Enrollment.Web.Implementation;
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Web;

namespace JX.Enrollment.Web.ViewModel
{
   
    public class StudentVM:BaseDA<Student>
    {
        public class Notification:Messages
        {
        }

        internal enum CommandArgs
        {
            //Enumeration of actions from the grid.
            View, EditRec, Documents, Workflow, DeleteRec
        }

        public static string NotificationMessage { get; set; }
        public static bool Delete(Int32 id)
        {
            bool result = false; //default value

            //Query from model Data Access Layer
            StudentDA studentDA = new StudentDA();
           
            Student student = studentDA.Where(c => c.ID == id).FirstOrDefault();
           

            if (student != null)
            {
                //Update the model student in form of action delete then back to controller to display(view)
                studentDA.Delete(student);
                NotificationMessage = Notification.Deleted;
                result = true;
            }
            else
            {
                result = false;
            }
            return result;
        }
        public static bool Insert(Student student)
        {
            bool result = false; //default value
            try
            {
                result = true;
                StudentDA studentDA = new StudentDA();
                studentDA.Insert(student);
                NotificationMessage = Notification.Inserted;
            }

            catch (Exception e)
            {
                result = false;
                NotificationMessage = e.Message;
            }
            return result;
        }
        public static bool  Update(Student student)
        {
            bool result = false; //default value
            try
            {
                result = true;
                student.ModifiedBy = "espirituj";
                student.ModifiedDate = DateTime.Now;

                StudentDA studentDA = new StudentDA();

                studentDA.Update(student);
                NotificationMessage = Notification.Updated;
            }

            //catch (Exception e)
            //{
            //    result = false;
            //    NotificationMessage = e.Message;
            //}

            catch (DbEntityValidationException dbEx)
            {
                result = false;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }
            return result;
        }
        public static Student Get(Int32 ID)
        {
            StudentDA studentDA = new StudentDA();

            Student item =  studentDA.Where(c => c.ID == ID).FirstOrDefault();
            return item;
        }
        public static IEnumerable<Student> GetStudentInfo()
        {
            // Get all data from model Data Access Layer then back to controller to update the display
            StudentDA studentDA = new StudentDA();
           
            IEnumerable<Student> list = studentDA.GetAll();

            // get necessary fields to display on grid
            IEnumerable<Student> students = (from b in list
                                             orderby b.ID descending
                                             select new Student
                                             {
                                                 ID = b.ID,
                                                 LastName = b.LastName,
                                                 FirstName = b.FirstName,
                                                 MiddleName = b.MiddleName,
                                                 Type = b.Type,
                                                 TempNo = b.TempNo,
                                                 PreviousSchool = b.PreviousSchool,
                                                 Relationship= b.Relationship,
                                                 CreatedDate = b.CreatedDate
                                             }).ToList();
            //result back to controller to update the display on View -gridview
            return students;
        }
        public static bool DisplayNotFound(IEnumerable<Student> studInfo)
        {
            bool result = false;
            result = (studInfo.Count() == 0 ? true : false);
            return result;
        }
        public static bool DisplayAddNewDeleteButton(IEnumerable<Student> studInfo)
        {
            bool result = false;
            result = (studInfo.Count() >= 5 ? true : false);
            return result;
        }
    }
}